<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择求卦类型" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.divination_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ted:divination:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-s-order"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ted:divination:edit']"
        >详情</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ted:divination:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['ted:divination:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="divinationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="求卦类型" align="center" prop="type" />-->
      <el-table-column label="求卦人名称" align="center" prop="nickName" />
      <el-table-column label="原卦" align="center" prop="oldD" />
      <el-table-column label="成卦" align="center" prop="newD" />
      <el-table-column label="成卦时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="愿" align="center" prop="mark" />
      <el-table-column label="爻词" align="center" prop="wordsD" />
      <el-table-column label="解卦批注" align="center" prop="note" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ted:divination:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ted:divination:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >查看批注</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改每日一卦对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div style="padding-top: 10px;">
<!--          <el-form-item label="类型" prop="type">-->
<!--            <el-input v-model="form.type" placeholder="请输入第一次" :disabled="true"/>-->
<!--          </el-form-item>-->
          <el-row style="margin-bottom: 10px; text-align: left;font-size: 28px;color: #3A71A8">
            <el-col :span="12">
              <div>内卦:</div>
            </el-col>
            <el-col :span="12">
              <div>外卦:</div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="第一次" prop="oneD">
                <div class="item-from">
                  <el-input v-model="form.oneD" placeholder="请输入第一次" :disabled="true"/>
                  <el-button  type="warning" plain size="mini" @click="form.oneD = clickD()" :disabled="form.oneD!=null">生成</el-button>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="第四次" prop="fourD">
                <div class="item-from">

                  <el-input v-model="form.fourD" placeholder="请输入第四次" :disabled="true"/>
                  <el-button type="warning" plain size="mini" v-if="form.threeD!=null" @click="form.fourD = clickD()" :disabled="form.fourD!=null">生成</el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="第二次" prop="twoD">
                <div class="item-from">
                  <el-input v-model="form.twoD" placeholder="请输入第二次" :disabled="true" />
                  <el-button type="warning" plain size="mini" v-if="form.oneD!=null"  @click="form.twoD = clickD()" :disabled="form.twoD!=null">生成</el-button>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="第五次" prop="fiveD">
                <div class="item-from">
                  <el-input v-model="form.fiveD" placeholder="请输入第五次" :disabled="true"/>
                  <el-button type="warning" plain size="mini" v-if="form.fourD!=null"  @click="form.fiveD = clickD()" :disabled="form.fiveD!=null">生成</el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="第三次" prop="threeD">
                <div class="item-from">
                  <el-input v-model="form.threeD" placeholder="请输入第三次" :disabled="true" />
                  <el-button type="warning" plain size="mini" v-if="form.twoD!=null"   @click="form.threeD = clickD()" :disabled="form.threeD!=null">生成</el-button>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="第六次" prop="sixD">
                <div class="item-from">
                  <el-input v-model="form.sixD" placeholder="请输入第六次" :disabled="true"/>
                  <el-button type="warning" plain size="mini" v-if="form.fiveD!=null"  @click="form.sixD = clickD()" :disabled="form.sixD!=null">生成</el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-form-item label="发愿" prop="mark">
          <el-input v-model="form.mark" type="textarea" placeholder="请输入内容" :rows="4" :readonly="readonly"/>
        </el-form-item>
        <div style="display: flex;justify-content: center; align-items: center;">
          <el-button type="success" plain @click="createD">生成卦象</el-button>
        </div>
        <el-divider content-position="right"></el-divider>

        <el-row>
          <el-col :span="12">
            <el-form-item label="原卦" prop="oldD">
              <el-input v-model="form.oldD" placeholder="请输入原卦" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成卦" prop="newD">
              <el-input v-model="form.newD" placeholder="请输入成卦" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="爻词" prop="wordsD">-->
<!--          <el-input v-model="form.wordsD" placeholder="请输入爻词" type="textarea" :rows="4"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="解卦批注" prop="note">-->
<!--          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" :rows="4"/>-->
<!--        </el-form-item>-->
      </el-form>
      <div style="color: #c49739">
        <div>说明： '0'代表'阴'(--);  '1'代表'阳'(—)</div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <detail :isShowDialog="isShow" @closeDialog="closeDialog" :formDataDialog="formData"></detail>
  </div>
</template>

<script>
import { listDivination, getDivination, delDivination, addDivination, updateDivination } from "@/api/ted/divination";
import detail from "./detail";
import {getDivinationFprDid} from "@/api/ted/dictDivination";

export default {
  name: "Divination",
  dicts: ['divination_type'],
  components:{detail},
  data() {
    return {
      //是否显示子组件
      isShow:false,
      //传递给子组件的对象
      formData:{},
      //是否只读
      readonly:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 每日一卦表格数据
      divinationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        oneD: null,
        twoD: null,
        threeD: null,
        fourD: null,
        fiveD: null,
        sixD: null,
        note: null,
        oldD: null,
        newD: null,
        wordsD: null,
        mark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        oneD: [{ required: true, message: "请生成卦象", trigger: "blur"}],
        twoD: [{ required: true, message: "请生成卦象", trigger: "blur"}],
        threeD: [{ required: true, message: "请生成卦象", trigger: "blur"}],
        fourD: [{ required: true, message: "请生成卦象", trigger: "blur"}],
        fiveD: [{ required: true, message: "请生成卦象", trigger: "blur"}],
        sixD: [{ required: true, message: "请生成卦象", trigger: "blur"}],
        oldD: [{ required: true, message: "请生成原卦", trigger: "blur"}],
        newD: [{ required: true, message: "请成卦", trigger: "blur"}],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询每日一卦列表 */
    getList() {
      this.loading = true;
      listDivination(this.queryParams).then(response => {
        this.divinationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        type: null,
        oneD: null,
        twoD: null,
        threeD: null,
        fourD: null,
        fiveD: null,
        sixD: null,
        note: null,
        oldD: null,
        newD: null,
        wordsD: null,
        mark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.readonly = false;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.readonly = false;
      this.reset();
      this.open = true;
      this.title = "添加每日一卦";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.readonly = true;
      this.reset();
      const id = row.id || this.ids
      getDivination(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDivination(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDivination(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除每日一卦编号为"' + ids + '"的数据项？').then(function() {
        return delDivination(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ted/divination/export', {
        ...this.queryParams
      }, `divination_${new Date().getTime()}.xlsx`)
    },
    //点击生成卦象
    clickD(){
      var x1 =  Math.floor(Math.random() * 100)%2;
      var x2 =  Math.floor(Math.random() * 100)%2;
      var x3 =  Math.floor(Math.random() * 100)%2;
      var data = '' + x1 + x2 + x3;
      return data;
    },
    //生成卦象
    createD(){
      if(this.form.oneD!=null&&this.form.twoD!=null&&this.form.threeD!=null&&
        this.form.fourD!=null&&this.form.fiveD!=null&&this.form.sixD!=null){
        this.form.oldD ="" + this.forD(this.form.oneD) + this.forD(this.form.twoD) + this.forD(this.form.threeD)
          +this.forD(this.form.fourD) + this.forD(this.form.fiveD) + this.forD(this.form.sixD);
        this.form.newD =''+ this.forD(this.form.oneD) + this.forD(this.form.twoD) + this.forD(this.form.threeD)
          +this.forD(this.form.fourD) + this.forD(this.form.fiveD) + this.forD(this.form.sixD);
      }else {
        throw new Error("请先生成全部卦象")
      }
    },
    forD(data){
      console.log("datassss",data)
      if(data == "001"||data == "111" || data =="010" || data =="100" ){
        return "0"
      }else if(data == "000" || data =="110" || data =="101" || data =="011"){
        return "1"
      }
    },
    //查看卦象批注
    handleDetail(row){
      getDivinationFprDid(row.newD).then(response=>{
        this.formData = response.data
        this.isShow = true
      })
    },
    //关闭弹窗
    closeDialog(){
      this.isShow = false;
    }
  }
};
</script>
<style>
.item-from{
  display: flex;
  justify-content: space-between;

}
</style>
